<?php

class Choice extends EMongoEmbeddedDocument
{
    public $id;
    public $text;
    public $answer;
    
    public function rules()
    {
        return array(
            array('text', 'required'),
            array('id, answer', 'safe'),
        );
    }
    
    public function afterValidate()
    {
        parent::afterValidate();
        if(empty($this->id))
            $this->id = md5(microtime(true).mt_rand(100, 999));
    }
    
    public function beforeToArray()
    {
        if( ! parent::beforeToArray())
            return false;
        
        if(empty($this->id))
            $this->id = md5(microtime(true).mt_rand(100, 999));
        
        return true;
    }
    
    public function getId()
    {
        return $this->id;
    }
    
    public function getDisplayText()
    {
        return $this->text;
    }
    
    public function getAnswer()
    {
        return $this->answer;
    }
}
